package ice;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import cli.Logging;
import cli.Settings;

public class Server {
	
	static DateFormat dateFormat = new SimpleDateFormat( Settings.DATE_FORMAT );
	
/*	
	static JFrame display = new JFrame("SSC-Server");
	private static JTextArea displayText = new JTextArea();
	static JScrollPane displayScroll = new JScrollPane(displayText);
*/
	public static String lastErrorMessage;

	
	
	public static void main(String[] args)
	{
/*
		display.add(displayScroll);
		display.setSize(400,200);
		display.setVisible(true);
*/		
		lastErrorMessage  = null;
		
		int status = 0;
		Ice.Communicator ic = null;
		try {
			ic = Ice.Util.initialize(args);
			Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints( "SimpleMipsAdapter", "default -p 10000");
			Ice.Object object = new MipsI();
			adapter.add( object, ic.stringToIdentity("SimpleMips"));
			adapter.activate();
			System.out.println( dateFormat.format( new Date() ) + " Server started" );
//			displayText.append(dateFormat.format( new Date() ) + " Server started\n");
			Logging.log("Connection", "Server started");
			ic.waitForShutdown();
		} catch (Ice.LocalException e) {
			e.printStackTrace();
			status = 1;
		} catch (Exception e) {
			System.err.println(e.getMessage());
			status = 1;
		}
		if (ic != null) {
			// Clean up
			//
			try {
				ic.destroy();
			} catch (Exception e) {
				System.err.println(e.getMessage());
				status = 1;
			}
		}
		System.exit(status);
	}
/*	
	public static void logMessageToScreen( String msg ) {
		displayText.append( msg + "\n");
	}
*/
	
	/**
	 * Set the last error message. When a client receives an indication that there <br>
	 * is an error then the client can  retrieve the 'lastErrorMessage'
	 * 
	 * @param msg
	 */
	public static void setLastErrorMessage( String msg ) {
		lastErrorMessage = msg;
	}

	/**
	 * Set the last error message. When the client receives an indication that there <br>
	 * is an error then the client can  retrieve the 'lastErrorMessage'
	 * 
	 * @param msg
	 * @return error message
	 */
	public static String getLastErrorMessage() {
		return lastErrorMessage;
	}
}